package com.xiaoyao.novel.crawl;

import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;

import com.xiaoyao.novel.parser.Go114Parser;
import com.xiaoyao.novel.pojo.Book;
import com.xiaoyao.novel.pojo.Chapter;
import com.xiaoyao.novel.util.ClassUtil;
import com.xiaoyao.novel.util.HttpClientUtil;

public class CopyOfGo114Crawl extends BaseCrawl {
//	private static HttpUtil httpBook = new HttpUtil("GBK");
	private static HttpClientUtil httpBook = new HttpClientUtil("GBK");
	private static Logger logger = Logger.getLogger(CopyOfGo114Crawl.class);

	public CopyOfGo114Crawl() {
		tag = Book.TYPE_GO114;
	}

	/**
	 * 爬一本书,目录
	 * 
	 * @param webId
	 */
	public void crawlBook(Book book) {
		String page = "";
		try {
			if (ClassUtil.isEmpty(book.describ)) {
				// http://www.bdzw.cc/files/article/info/23/23566.htm   ///简介
				String describUrl = "http://www.144go.com/modules/article/articleinfo.php?id=" + book.webId;
				logger.info("读取简介:" + describUrl);
				page = httpBook.getPage(describUrl);
				Go114Parser.parseDescrib(page, book);
			}
			// http://www.piaotian.net/html/4/4277/
			String categoryUrl = book.bookUrl;
			categoryUrl = "http://www.144go.com/files/article/html/9/9154/index.html";
			logger.info("读取目录:" + categoryUrl);
			// page = httpBook.getPage(categoryUrl,headMap);
			page = httpBook.getPage(categoryUrl);
			logger.debug(page);
			List<Chapter> list = Go114Parser.parseBook(page, book);
			for (int i = book.savedCount; i < list.size(); i++) {
				Chapter chapter = book.list.get(i);
				crawlChapter(chapter);
			}
		} catch (Exception e) {
			logger.error(e);
		}
	}
	/**
	 * 爬章节类容
	 * 
	 * @param chapter
	 * @throws IOException
	 */
	public void crawlChapter(Chapter chapter) throws IOException {
		// http://www.piaotian.net/html/4/4363/2243016.html
		String urlStr = chapter.chapterUrl;
		logger.info("爬取章节 :[" + urlStr + "]");

		String page = "";
		for (int i = 0; i < 3 && ClassUtil.isEmpty(page); i++) {
			try {
				page = httpBook  .getPage(urlStr);
			} catch (Exception e) {
				// e.printStackTrace();
				logger.error(e);
			}
		}
		page = filter(page);
		Go114Parser.parseChapter(page, chapter);
		logger.info("内容长度:" + chapter.chapterContent.length());
	}

	// ///////////////////////////////////////////
	// ///////////////////////////////////////////
	/**
	 * 过滤
	 * 
	 * @param page
	 * @return
	 */
	public String filter(String page) {
		// page = page.replaceAll("<img.+?>", "");
		page = page.replaceAll("www.144go.com", "");
		page = page.replaceAll("go114", "");
		page = page.replaceAll("114", "");
		// page = page.replaceAll("&.{2,7};", " ");
		// page = page.replaceFirst("<a href=http://www.qidian.com>.+", "");
		// 其他替换
		return page;
	}

	public static void main(String[] args) {
		Book book = new Book();
		book.bookId = 63;
		book.webId = "9154"; //
		CopyOfGo114Crawl crawl = new CopyOfGo114Crawl();
		crawl.crawlBook(book);
	}

}
